﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;


using LRSTaxServices.Biz;
using LRSTaxServices.Common;

public partial class Controls_TaxDocumentsViewEdit : System.Web.UI.UserControl
{
    #region Constants

    private const string PROP_MODE = "__TaxDocumentsViewEdit::Mode";

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets / sets the TaxDocuments data source object.
    /// </summary>
    public TaxDocuments DataSourceTaxDocuments
    {
        get
        {
            if (_dataSource == null)
            {
                if (DataSourceTaxDocumentsRequested != null)
                    DataSourceTaxDocumentsRequested(this, EventArgs.Empty);
            }
            return (_dataSource);
        }
        set
        {
            if (_dataSource != value)
            {
                _dataSource = value;
            }
        }
    } private TaxDocuments _dataSource = null;

    /// <summary>
    /// Gets / sets the control's mode.
    /// </summary>
    public ViewEditMode Mode
    {
        get { return (ViewState[ClientID + PROP_MODE] is ViewEditMode ? (ViewEditMode)ViewState[ClientID + PROP_MODE] : ViewEditMode.View); }
        private set { ViewState[ClientID + PROP_MODE] = value; }
    }

    #endregion

    #region Public Events

    public event EventHandler DataSourceTaxDocumentsRequested;

    #endregion

    #region Public Methods

    /// <summary>
    /// Method initializes and binds the controls UI elements to the properties of the specified object instance.
    /// </summary>
    public bool DataBind(TaxDocuments taxdocuments)
    {
        return (DataBind(taxdocuments, Mode));
    }


    /// <summary>
    /// Method initializes and binds the controls UI elements to the properties of the specified object instance.
    /// </summary>
    public bool DataBind(TaxDocuments taxdocuments, ViewEditMode mode)
    {
        DataSourceTaxDocuments = taxdocuments;
        Mode = mode;

        SetLayout(Mode);

        UpdateData(UpdateDirection.Obj_To_UI);

        return (true);
    }


    /// <summary>
    /// Method causes the control to update its DataSource(s) with the values in the UI elements.
    /// </summary>
    public bool EndEdit()
    {
        bool flag = false;
        if (uploaddocs.HasFile)
        {
            string filename = "";
            filename = uploaddocs.FileName;
            string fileextension = Path.GetExtension(filename);
            if (fileextension.Equals(".doc") || fileextension.Equals(".docx") || fileextension.Equals(".xls") || fileextension.Equals(".xlsx") || fileextension.Equals(".pdf"))
            {
                uploaddocs.SaveAs(Server.MapPath("../TaxDocuments" + "\\" + filename));
                lblerrormsg.Visible = false;
            }
            else
            {
                lblerrormsg.Visible = true;
                lblerrormsg.Text = "you can upload only .doc,.docx,xls,xlsx and.pdf formet supported files.";
            }
            TaxDocuments taxdocumentsobjname = TaxDocuments.GetTaxDocumentsByUploadedFileName(filename);
            if (taxdocumentsobjname == null || filename == DataSourceTaxDocuments.UploadedFileName && lblerrormsg.Visible == false)
            {
                flag = true;
            }
        }
        if (flag == true && lblerrormsg.Visible == false)
        {
            UpdateData(UpdateDirection.UI_To_Obj);
            return (true);
        }
        else
            return (false);

    }


    /// <summary>
    /// Method causes the control to reject changes made in the UI by rebinding the UI elements to the DataSource(s).
    /// </summary>
    public void CancelEdit()
    {
        UpdateData(UpdateDirection.Obj_To_UI);
    }


    /// <summary>
    /// Method sets the control's ViewEditMode.
    /// </summary>
    public void SetViewEditMode(ViewEditMode mode)
    {
        Mode = mode;
        SetLayout(Mode);
    }

    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs evt)
    {
        if (!Page.IsPostBack)
        {
            //InitMaxFieldLengths();
        }
    }

    #endregion

    #endregion

    #region Privates

    #region SetLayout

    private void SetLayout(ViewEditMode mode)
    {
        switch (mode)
        {
            case ViewEditMode.View:
                break;

            case ViewEditMode.Edit:
                break;
        }
    }

    #endregion

    #region InitMaxFieldLengths

    private void InitMaxFieldLengths()
    {
        //Process to initialize the maxlenght values to the control on UI
    }

    #endregion

    #region UpdateDate

    public void UpdateData(UpdateDirection updatedir)
    {
        if (DataSourceTaxDocuments == null)
            return;

        bool addingNew = (DataSourceTaxDocuments.ObjState == BOBase.ObjectState.Added);

        if (updatedir == UpdateDirection.Obj_To_UI)
        {
            //View Elements





            //Edit Elements

            txtdocname.Text = DataSourceTaxDocuments.DocumentName;
            txtdescription.Text = DataSourceTaxDocuments.DocumentDescription;
        }
        else
        {
            DataSourceTaxDocuments.DocumentName = txtdocname.Text.Trim();
            if (uploaddocs.HasFile)
            {
                DataSourceTaxDocuments.UploadedFileName = uploaddocs.FileName;
            }
            else
            {
                DataSourceTaxDocuments.UploadedFileName = DataSourceTaxDocuments.UploadedFileName;
            }
            DataSourceTaxDocuments.DocumentDescription = txtdescription.Text.Trim();
        }

    }

    #endregion

    #endregion
}
